﻿
using Confluent.Kafka;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Ccteg.Common
{
    public static class KafakHelper
    {
        public static ResponseModel<string> Producer(string topic, string jsondata)
        {
            var response = new ResponseModel<string>();
            response.SetFailed(); //默认为失败
            try
            {
                var pConfig = new ProducerConfig
                {
                    //BootstrapServers = "61.132.89.53:3090",
                    BootstrapServers = "47.100.173.18:9092",
                };

                using (var producer = new ProducerBuilder<Null, string>(pConfig).Build())
                {
                    var task = producer.ProduceAsync(topic, new Message<Null, string> { Value = jsondata }, new CancellationTokenSource(5000).Token).GetAwaiter().GetResult();
                    response.SetSuccess(task.Value);
                }

            }
            catch (Exception ex)
            {
                response.Code = 100;
                response.Message = ex.Message;

            }
            return response;
        }
    }
}
